home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 43 / Amiga Format CD43 (1999)(Future Publishing)(GB)(Track 1 of 2)[!][issue 1999-09].iso / -serious- / comms / other / ftransapiv / sourcecode / ftrans.c < prev    next >
C/C++ Source or Header  |  1999-06-14  |  10KB  |  341 lines

  1. #include "surround.h"
  2. #include <exec/types.h>
  3. #include <intuition/intuition.h>
  4. #include <intuition/IntuitionBase.h>
  5. #include <intuition/classusr.h>
  6. #include <intuition/pointerclass.h>
  7. #include <intuition/gadgetclass.h>
  8. #include <intuition/sghooks.h>
  9. #include <intuition/icclass.h>
  10. #include <graphics/displayinfo.h>
  11. #include <graphics/text.h>
  12. #include <graphics/gfx.h>
  13. #include <libraries/dos.h>
  14. #include <libraries/gadtools.h>
  15. #include <libraries/diskfont.h>
  16. #include <libraries/asl.h>
  17. #include <clib/exec_protos.h>
  18. #include <clib/intuition_protos.h>
  19. #include <clib/dos_protos.h>
  20. #include <clib/graphics_protos.h>
  21. #include <clib/gadtools_protos.h>
  22. #include <clib/asl_protos.h>
  23. #include <clib/datatypes_protos.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <dos/dos.h>
  28. #include <dos/dosasl.h>
  29. #include <exec/exec.h>
  30. #include <proto/exec.h>
  31. #include <proto/utility.h>
  32. #include <proto/datatypes.h>
  33. #include <datatypes/datatypesclass.h>
  34. #include <datatypes/pictureclass.h>
  35. #include <datatypes/animationclass.h>
  36. #include <datatypes/datatypes.h>
  37. #include <errno.h>
  38. #include <netdb.h>
  39. #include <sys/types.h>
  40. #include <sys/time.h>
  41. #include <sys/socket.h>
  42. #include <sys/ioctl.h>
  43. #include <netinet/in.h>
  44. #include <proto/socket.h>
  45. #include <signal.h>
  46.  
  47. // INFORMACOES DA VERSAO
  48. #define VERS "Freedom Internet Translator Client API v1.0"
  49. #define DATE "01.05.1999"
  50. #define VSTRING VERS " (" DATE ")"
  51. #define VERSTAG "\\0$VER: " VERS " (" DATE ")"
  52. const static char VersTag[] = VERSTAG;
  53.  
  54. // NUMERO MAXIMO DE SOCKETS ABERTOS
  55. #define MAXSOCKS 10L
  56.  
  57. // TAMANHO DO BUFFER DE LEITURA DE COMUNICACAO
  58. int TempBufferSize=65535;
  59.  
  60. // PONTO DE ACESSO A UM HOST
  61. struct hostent *hptr;
  62. struct sockaddr_in server;
  63. int byteslidos,result,totalbytes;
  64. unsigned char  *enderecoip;
  65. BPTR ArquivoCache=NULL;
  66. char *TempBuffer=NULL;
  67. char TempString[256], status[256];
  68. LONG TipoEndereco;
  69. char NomeDoHost[256];
  70. char direcao[50];
  71. int socket1;
  72.  
  73. void main(argc, argv)
  74. int argc;
  75. char *argv[];
  76. {
  77.   char *linha, *linhaf, *requisicao,
  78.        opcao[10], lingua[10], tamanho[50], a,
  79.        *saida, *teste, tmpfile[60];
  80.   int  x = 0, silence = 0;
  81.   FILE *entrada;
  82.  
  83.   // ACERTA O NOME DO ARQUIVO TEMPORARIO
  84.   sprintf(tmpfile, "Ram:FreeTmp.%s", DataHora(7));
  85.  
  86.   // TESTA OPCAO SILENCE
  87.   if(!(strcmp(strlwr(argv[3]), "silence")) || !(strcmp(strlwr(argv[4]), "silence")))
  88.     silence = 1;
  89.  
  90.   // TITULO DO PROGRAMA
  91.   if(silence == 0)
  92.   {
  93.     printf("\nFreedom Internet Translator API v1.0 [01.05.1999]\n");
  94.     printf("By Cristian Robert Gallas (SuRRouND)\n");
  95.     printf("Lithium Software 1999(r)\n");
  96.     printf("FREEWARE WITH SOURCE FREE - CONTRIBUTE TO AMIGA OPEN SOURCE!\n\n");
  97.   } else {
  98.     printf("\n"); }
  99.  
  100.   strcpy(opcao, argv[1]);
  101.   if(!strcmp(strlwr(opcao), "help"))
  102.   {
  103.     printf("Sintax:\nFTranslator [FILE|TEXT|HELP] [DIRECTION] \"text or txtfile\" [SILENCE]\n\n");
  104.     printf("DIRECTION: 0 - English   to Francais\n");
  105.     printf("           1 - English   to Deutsch\n");
  106.     printf("           2 - English   to Italiano\n");
  107.     printf("           3 - English   to Portugues\n");
  108.     printf("           4 - English   to Espanol\n");
  109.     printf("           5 - Francais  to English\n");
  110.     printf("           6 - Deutsch   to English\n");
  111.     printf("           7 - Italiano  to English\n");
  112.     printf("           8 - Espanol   to English\n");
  113.     printf("           9 - Portugues to English\n\n");
  114.     exit(0);
  115.   }
  116.  
  117.   if(argc < 3)
  118.   {
  119.     printf("Sintax:\nFTranslator [FILE|TEXT|HELP] [DIRECTION] \"text or txtfile\" [SILENCE]\n\n");
  120.     exit(0);
  121.   }
  122.  
  123.   // VERIFICA A LINGUA A TRADUZIR
  124.   switch (atoi(argv[2]))
  125.   {
  126.     case 0: strcpy(lingua, "en_fr"); 
  127.             strcpy(direcao, "English to Francais"); break;
  128.     case 1: strcpy(lingua, "en_de");
  129.             strcpy(direcao, "English to Deutsch"); break;
  130.     case 2: strcpy(lingua, "en_it");
  131.             strcpy(direcao, "English to Italiano"); break;
  132.     case 3: strcpy(lingua, "en_pt");
  133.             strcpy(direcao, "English to Portugues"); break;
  134.     case 4: strcpy(lingua, "en_es");
  135.             strcpy(direcao, "English to Espanol"); break;
  136.     case 5: strcpy(lingua, "fr_en");
  137.             strcpy(direcao, "Francais to English"); break;
  138.     case 6: strcpy(lingua, "de_en");
  139.             strcpy(direcao, "Deutsch to English"); break;
  140.     case 7: strcpy(lingua, "it_en");
  141.             strcpy(direcao, "Italiano to English"); break;
  142.     case 8: strcpy(lingua, "es_en");
  143.             strcpy(direcao, "Espanol to English"); break;
  144.     case 9: strcpy(lingua, "pt_en");
  145.             strcpy(direcao, "Portugues to English"); break;
  146.   }
  147.  
  148.   // ROTINA QUE LE O CONTEUDO DO TEXT
  149.   if(!strcmp(strlwr(opcao), "text"))
  150.   {
  151.     // ALOCA MEMORIA PARA AS VARIAVEIS DE PEDIDO
  152.     x = (strlen(argv[3]) + 500);
  153.     linhaf     = malloc(x);
  154.     linha      = malloc(x);
  155.     requisicao = malloc(x);
  156.     linha[0] = '\0'; requisicao[0] = '\0';
  157.  
  158.     strcpy(linhaf, argv[3]);
  159.     x = strlen(argv[3]);
  160.     linhaf[x] = '\0';
  161.   }
  162.  
  163.   // ROTINA QUE LE O CONTEUDO DE UM ARQUIVO TX 
  164.   if(!strcmp(strlwr(opcao), "file"))
  165.   {
  166.     // ROTINA QUE LE O ARQUIVO E JOGA NA VARIAVEL LINHA
  167.     if(!(entrada = fopen(argv[3], "r")))
  168.     {
  169.       printf("%s: Access denied or file not found...\n\n", argv[3]);
  170.       exit(0);
  171.     }
  172.     // ALOCA MEMORIA PARA AS VARIAVEIS DE PEDIDO
  173.     x = (atoi(ExaminaArquivo(argv[3], 0)) + 500);
  174.     linhaf     = malloc(x);
  175.     linha      = malloc(x);
  176.     requisicao = malloc(x);
  177.     linhaf[0] = '\0'; linha[0] = '\0'; requisicao[0] = '\0';
  178.  
  179.     // LE O ARQUIVO E JOGA NA VARIAVEL PARA TRADUCAO
  180.     x = 0;
  181.     while(a = getc(entrada),! feof(entrada))
  182.       linhaf[x++] = a;
  183.     linhaf[x] = '\0';
  184.     fclose(entrada);
  185.   } 
  186.  
  187.   // INICIA A CONECCAO NO SERVIDOR ABRINDO A SOCKET
  188.   SockBase=(struct Library *)OpenLibrary("socket.library",0L);
  189.   if(!(SockBase))
  190.   {
  191.     printf("\nYou need connect on internet first...\n\n");
  192.     exit(0);
  193.   }
  194.   if(silence == 0)
  195.     printf("Open Socket...\n");
  196.  
  197.   setup_sockets(MAXSOCKS,&errno);
  198.  
  199.   // PEGA INFORMACOES SOBRE O HOST
  200.   if(!(hptr=gethostbyname("babelfish.altavista.digital.com")))
  201.   {
  202.     printf("\nProblem on translator server, try again later...\n\n");
  203.     remove(tmpfile);
  204.     goto SAIDAGOTO;
  205.   }
  206.   memcpy((char *)&server.sin_addr,(char *)hptr->h_addr,hptr->h_length);
  207.   // AF_INET = INTERNET
  208.   server.sin_family=AF_INET;
  209.  
  210.   // PORTA 80 (SERVIDOR WEB)
  211.   server.sin_port=80;
  212.  
  213.   TipoEndereco=hptr->h_addrtype;
  214.   strcpy(NomeDoHost,hptr->h_name);
  215.   enderecoip=(unsigned char *)hptr->h_addr_list[0];
  216.   socket1=socket(hptr->h_addrtype, SOCK_STREAM,0);
  217.  
  218.   if(silence == 0)
  219.     printf("Contacting host...\n");
  220.  
  221.   // CONECTA O SOCKET AO ENDERECO
  222.   result=connect(socket1,(struct sockaddr*)&server,sizeof(server));
  223.   if(result == -1)
  224.   {
  225.     printf("\nConnection problem, try again later...\n\n");
  226.     goto SAIDAGOTO;
  227.   }
  228.   if(silence == 0)
  229.     printf("Host contacted. Now Doing request...\n");
  230.  
  231.   // ROTINA QUE MONTA O PEDIDO DE TRADUCAO
  232.   sprintf(linha, "doit=done&urltext='%s'&languagepair=%s", linhaf, lingua);
  233.   sprintf(tamanho, "Content-Length: %d", strlen(linha));
  234.  
  235.   // INICIALIZA A REQUISICAO
  236.   requisicao[0]='\0';
  237.   strcpy(requisicao,"POST /cgi-bin/translate? HTTP/1.0");
  238.   strcat(requisicao,"\x0d\x0a\0");
  239.   strcat(requisicao,"User-Agent: FreedomTranslator/1.0; (Amiga Translator Client)");
  240.   strcat(requisicao,"\x0d\x0a\0");
  241.   strcat(requisicao,"Accept: */*;q=1");
  242.   strcat(requisicao,"\x0d\x0a\0");
  243.   strcat(requisicao,"Host: babelfish.altavista.digital.com");
  244.   strcat(requisicao,"\x0d\x0a\0");
  245.   strcat(requisicao, tamanho);
  246.   strcat(requisicao,"\x0d\x0a\0");
  247.   strcat(requisicao,"Content-Type: application/x-www-form-urlencoded");
  248.   strcat(requisicao,"\x0d\x0a\0");
  249.   strcat(requisicao,"\x0d\x0a\0");
  250.   strcat(requisicao, linha);
  251.   strcat(requisicao,"\x0d\x0a\0");
  252.   strcat(requisicao,"\x0d\x0a\0");
  253.  
  254.   // ENVIA A REQUISICAO DE HTTP
  255.   result=send(socket1,requisicao,strlen(requisicao),0);
  256.   if(silence == 0)
  257.     printf("Done http request. Waiting... ");
  258.  
  259.   totalbytes=0;
  260.   if(!(ArquivoCache = Open(tmpfile, MODE_NEWFILE)))
  261.   {
  262.     printf("\nFTranslator port is open, binary FTranslator is in use, wait...\n\n");
  263.     goto SAIDAGOTO;
  264.   }
  265.   TempBuffer=AllocMem(TempBufferSize,MEMF_FAST|MEMF_CLEAR);
  266.   if(!(TempBuffer))
  267.   {
  268.     printf("Not have memory for allocate temporary buffer...\n");
  269.     remove(tmpfile);
  270.     goto SAIDAGOTO;
  271.   }
  272.   do
  273.   {
  274.     byteslidos  = 0;
  275.     *TempBuffer = '\0';
  276.     byteslidos  = recv(socket1, TempBuffer, TempBufferSize-1,0);
  277.     if(byteslidos)
  278.     {
  279.       Write(ArquivoCache, TempBuffer, byteslidos);
  280.       totalbytes += byteslidos;
  281.       TempString[0] = '\0';
  282.     }
  283.   }
  284.   while(byteslidos>0);
  285.  
  286.   // FECHA O SOCKET E O ARQUIVO
  287.   s_close(socket1);
  288.   Close(ArquivoCache);
  289.   FreeMem(TempBuffer,TempBufferSize);
  290.   cleanup_sockets();
  291.   CloseLibrary((struct Library *)SockBase);
  292.  
  293.   // ETAPA FINAL DO PROGRAMA
  294.   if(silence == 0)
  295.     printf("Done.\n");
  296.   if(!(ExisteArquivo(tmpfile)))
  297.   {
  298.     printf("%s: Problem on temporary file...\n\n", tmpfile);
  299.     exit(0);
  300.   }
  301.  
  302.   saida=malloc(atoi(ExaminaArquivo(tmpfile, 0))+500);
  303.   teste=malloc(atoi(ExaminaArquivo(tmpfile, 0))+500);
  304.  
  305.   strcpy(saida, CortaStringArquivo(tmpfile, "<font face=\"arial, helvetica\">", "<br><br></font></td></tr>", 2));
  306.  
  307.   // FILTRA LINHA PARA MELHORAR A SAIDA DA TRADUCAO
  308.   if(silence == 0)
  309.     printf("Applying filters... ");
  310.  
  311.   strcpy(teste, SubstituiString(saida, "<P>", "\n"));
  312.   strcpy(saida, teste);
  313.   strcpy(teste, SubstituiString(saida, "<", ""));
  314.   strcpy(saida, teste);
  315.   strcpy(teste, SubstituiString(saida, ">", ""));
  316.   strcpy(saida, teste);
  317.   strcpy(teste, SubstituiString(saida, "`", " "));
  318.   strcpy(saida, teste);
  319.   strcpy(teste, SubstituiString(saida, "'", " "));
  320.   strcpy(saida, teste); 
  321.   strcpy(teste, SubstituiString(saida, "\"", " "));
  322.   strcpy(saida, teste); 
  323.  
  324.   if(silence == 0)
  325.     printf("Done.\n\n");
  326.  
  327.   // MOSTRA A TRADUCAO
  328.   printf(" Translation: %s\n", direcao);
  329.   printf("o----------------------------------------------o\n");
  330.   printf("%s\n", saida);
  331.   printf("o----------------------------------------------o\n");
  332.   printf(" FTranslator API by Cristian Robert Gallas v1.0 \n");
  333.   printf(" Freeware with Source Free  (Amiga Open Source) \n");
  334.   printf("o----------------------------------------------o\n");
  335.  
  336.   // FINALIZA O PROGRAMA
  337.   SAIDAGOTO:
  338.   remove(tmpfile); 
  339.   exit(0);
  340. }                                           
  341.